<?php
namespace Manager\Controller;
use Org\Util\Ueditor;
use Think\Page;
use Think\Upload;
use Think\Image;
/**
* ==============================================
* URL：http://www.liuhuanhui.cn
* ==============================================
* @date：2014-10-20
* @time：下午10:30:28
* @author：liuhuanhui
* @encoding：UTF-8
*/
class ArticleController extends  AuthController
{
	/**
	 * 文章列表
	 */
	function  index()
	{
		if(I('key')!=null)
			$where['title']=array('like','%'.I('key').'%');
		if(I('sort_id')!=null)
			$where['sort_id']=I('sort_id');
		//分页获取数据
		$blog=D('BlogsView');
		$where['state']=array('neq','remove');
		$page=new Page($blog->where($where)->count(),10);
		$this->blogs=D('BlogsView')->where($where)->limit($page->firstRow,$page->listRows)->order('date desc')->select();
		$this->pageShow=$page->show();

		//获取文章分类
		$this->sorts=M('sorts')->select();
		$this->display();
	}
	/**
	 * 添加文章和编辑文章
	 */
	function  create()
	{
		if(I('id'))
		{
			$this->blog=M('Blogs')->find(I('id'));
		} 
		//获取文章分类
		$this->sorts=M('sorts')->select();
		$this->display();
	}
	/**
	 * 添加文章处理
	 */
	function createHandle()
	{
		$_POST['date']=strtotime(I('date'));
		
		if(!empty($_FILES['file']['name']))
			{
				/********SAE*******/
				$upload=new Upload();
				$upload->savePath='/article_images/';
				$upload->autoSub=false;
				$upload->replace=true;
				$info=$upload->uploadOne($_FILES['file']);
				
				//生成缩略图
				$s = new \SaeStorage();
				$f = new \SaeFetchurl();
				$img= new \SaeImage();
				$first_img_pic= $info['url'];
				$img_data= $f->fetch( $first_img_pic);
				$img->setData($img_data);
				$img->resize(400);// 等比缩放
				$Smallimg= $img->exec(); // 执行处理并返回处理后的二进制数据
				$s->write('uploads',"article_images/".$info['savename'],$Smallimg, -1);
				$_POST['image']=$info['savename'];
				//本地
// 				$fileName='Cover_'.time().'.'.pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
// 				$savePath='uploads/article_images/'.$fileName;
// 				$image = new Image();
// 				$image->open($_FILES['file']['tmp_name']);
// 				$result=$image->thumb(400,400)->save($savePath);
// 				if (!$result)
// 					$this->error('图片上传失败！');
				//$_POST['image']=$fileName;
				
		}
			$Article=D('Blogs');
			if($Article->create($_POST))
			{
				if(!I('id'))
				{
					if($Article->add())
					{
						$this->success("添加文章成功 ,点击返回文章列表",U(index));
					}else
					{
						$this->error("添加文章出错!");
					}
				}else{
					//if(empty($_POST['image']))
						//$_POST['image']=$Article->where(array(id=>I('id')))->getField('image');
					if($Article->save())
					{
						$this->success("修改文章成功 ,点击返回文章列表",U(index));
					}else
					{
						$this->error("修改文章出错!");
					}
				}
			}else
			{
				exit($Article->getError());
			}
	}
	/**
	 * 批量移动
	 */
	function moves()
	{
		$arr=join($_POST['selectIDS'], ',');
		$where['id']=array('in',$arr);
		if(M('blogs')->where($where)->setField('sort_id',I('move_sort_id')))
		{
			$this->success("批量移动分类成功！");
		}else
		{
			
			$this->error("批量移动分类失败!");
		}
	}
	/**
	 *删除文章信息
	 */
	function delete()
	{
		if(I('id'))
		{
			if(M('blogs')->delete(I('id')))
			{
				$this->success("删除文章成功 ,点击返回文章列表",U(index));
			}else
			{
				$this->error("删除文章失败!");
			}
		}
	}
	/**
	 * 移动到回收站
	 */
	function remove()
	{
        
		if(I('id'))
		{	
			$data=array(
				id=>I('id'),
				state=>'remove',
			);
			if(M('blogs')->save($data))
			{
				$this->success("文章已经移动到回收站 ,点击返回文章列表！");
			}else
			{
                $this->error("移动文章到回收站失败!");
			}
		}
	}
	/**
	 * 把选择的移到回收站
	 */
	function removes()
	{

		$arr=join($_POST['selectIDS'], ',');
		$where['id']=array('in',$arr);
		
		if(M('blogs')->where($where)->setField('state','remove'))
		{
			$this->success("批量移动回收站成功！");
		}else
		{
             $this->error("批量移动回收站失败!");
		}
		
	
	}
	/**
	 * 编辑器
	 */
	function ueditor()
	{
		$data =new Ueditor();
		//$data->rootpath='./test';
		echo $data->output();
	}
	/**
	 * 推荐的文章
	 */
	function recommend()
	{		
		//分页获取数据
		$blog=D('BlogsView');
		$where['istop']=array('eq','y');
		$page=new Page($blog->where($where)->count(),10);
		$this->blogs=D('BlogsView')->where($where)->limit($page->firstRow,$page->listRows)->order('date desc')->select();
		$this->pageShow=$page->show();
		$this->display();
	}
	/**
	 * 取消推荐
	 */
	function cancelRecommend()
	{
		if (I('id'))
		{
			if(M('blogs')->where(array(id=>I('id')))->setField('istop','n'))
			{
				$this->success("取消推荐成功!");
			}else
			{
				$this->error("取消推荐失败!");
			}
		}
	}
	/**
	 * 添加推荐
	 */
	function addRecommend()
	{
		if (I('id'))
		{
			if(M('blogs')->where(array(id=>I('id')))->setField('istop','y'))
			{
				$this->success("推荐成功!");
			}else
			{
				$this->error("推荐失败!");
			}
		}
	}
}